/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.mapper.project;

import cn.ac.nercis.pes.common.utils.ConvertUtils;
import cn.ac.nercis.pes.common.constant.project.ProjectState;
import cn.ac.nercis.pes.model.dto.project.ProjectDTO;
import cn.ac.nercis.pes.model.dto.project.ProjectDetailDTO;
import cn.ac.nercis.pes.repository.model.project.ProjectDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.Objects;
import java.util.Optional;

/**
 * 项目信息类型映射
 *
 * @author zpy
 * @version 1.0.0
 */
@Mapper(componentModel = "spring",
        imports = {Optional.class,
                Objects.class,
                ProjectState.class,
                ConvertUtils.class},
        unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ProjectMapper {
    ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class);

    /**
     * 转换为DTO类型项目信息
     * @param entity DO类型项目信息
     * @return 返回DTO类型项目信息
     */
    @Mapping(target = "state",expression = "java(Optional.ofNullable(entity).map(ProjectDO::getState).map(ProjectState::getName).orElse(\"\"))")
    @Mapping(target = "systemCount",expression = "java(Optional.ofNullable(entity).filter(o-> Objects.nonNull(o.getSystems())).map(o->o.getSystems().size()).orElse(0))")
    @Mapping(target = "createTime",expression = "java(Optional.ofNullable(entity).map(o->ConvertUtils.toString(o.getCreateTime())).orElse(\"\"))")
    ProjectDTO toDTO(ProjectDO entity);

    ProjectDO toDO(ProjectDetailDTO dto);

}